package com.learn.concurrent;

import java.util.concurrent.*;

/**
 * @author wangxing
 * @version 2020/8/1 6:41 Administrator
 */
public class CreateThread {
    /**
     * 继承Thread创建线程
     */
    public void createThreadThread(){
        Thread t =new Thread(){
            @Override
            public void run() {
                System.out.printf("继承Thread创建线程%s",Thread.currentThread().getName());
            }
        };
        t.start();
    }
    public void createThreadRunnable(){
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.printf("实现Runnable接口创建线程%s",Thread.currentThread().getName());
                    System.out.println(1/0);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        Thread t = new Thread(r);
        t.start();
    }
    public void createThreadCallable(String message){
        Callable<String> callable = new Callable() {
            @Override
            public String call() throws Exception {
                System.out.printf("实现Callable接口创建线程%s",Thread.currentThread().getName());
                try{
                    System.out.println(1/0);
                }catch (Exception e){
                    e.printStackTrace();
                }
                return "实现Callable接口创建线程,可以返回参数进行执行"+message;
            }
        };
        FutureTask<String> futureTask = new FutureTask<>(callable);
        Thread thread = new Thread(futureTask);
        thread.start();
        try {
            System.out.printf("-----------%s---------",futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    public void createThreadExecutors(){
        System.out.println("使用线程池Executors创建线程");
        ExecutorService executorService =null;
        String msg="";
        msg="---------------newCachedThreadPool----------------";
        executorService = Executors.newCachedThreadPool();runThreadExecutors(executorService,msg);
        msg="--------------newSingleThreadExecutor-------------";
        executorService = Executors.newSingleThreadExecutor();runThreadExecutors(executorService,msg);
        msg="--------------newFixedThreadPool 1-------------";
        executorService = Executors.newFixedThreadPool(1);runThreadExecutors(executorService,msg);
        msg="--------------newFixedThreadPool 2-------------";
        executorService = Executors.newFixedThreadPool(2);runThreadExecutors(executorService,msg);
        msg="--------------newFixedThreadPool 10-------------";
        executorService = Executors.newFixedThreadPool(10);runThreadExecutors(executorService,msg);
        msg="--------------newWorkStealingPool-------------";
        executorService = Executors.newWorkStealingPool();runThreadExecutors(executorService,msg);
    }
    private void runThreadExecutors(ExecutorService executorService,String msg){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(msg);
        for (int i = 0; i < 6; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("使用线程池创建线程"+Thread.currentThread().getName());
                }
            });
        }
        executorService.shutdown();
    }

}
